//
//  FirstViewController.m
//  BYR
//
//  Created by Bob on 15/3/15.
//  Copyright (c) 2015年 Bob. All rights reserved.
//

#import "FirstViewController.h"
#import "LoginView.h"
#import "MJRefresh.h"
@interface FirstViewController ()
@property LoginView *logview;

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.automaticallyAdjustsScrollViewInsets = YES;
   // self.tabBarItem->_action = @selector(TEST);

    [self addScrollContentView];
    [self addTableViewHeader];
    [self addTableViewFooter];
    // 马上进入刷新状态
   // [self.firstTable.gifHeader beginRefreshing];
}

-(void)addScrollContentView{
    self.ScrollContent                                = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    self.ScrollContent.contentSize                    = CGSizeMake(self.ScrollContent.bounds.size.width*2, self.ScrollContent.bounds.size.height-113);
    [self.view addSubview:self.ScrollContent];
    self.firstTable                                   = [[UITableView alloc]initWithFrame:self.ScrollContent.bounds];
    self.secondTable                                  = [[UITableView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    [self.ScrollContent addSubview:self.firstTable];
    [self.ScrollContent addSubview:self.secondTable];
    self.ScrollContent.pagingEnabled                  = YES;
    self.ScrollContent.showsHorizontalScrollIndicator = NO;
    self.ScrollContent.showsVerticalScrollIndicator   = NO;
}

-(void)addTableViewHeader{
    [self.firstTable addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 设置普通状态的动画图片
    NSMutableArray *idleImages = [NSMutableArray array];
    UIImage *image = [UIImage imageNamed:@"normal.png"];
    [idleImages addObject:image];
    [self.firstTable.gifHeader setImages:idleImages forState:MJRefreshHeaderStateIdle];
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
    NSMutableArray *refreshingImages = [NSMutableArray array];
    image = [UIImage imageNamed:@"pulling.png"];
    [refreshingImages addObject:image];
    [self.firstTable.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStatePulling];
    NSMutableArray *finishImages = [NSMutableArray array];
    image = [UIImage imageNamed:@"finish.png"];
    [finishImages addObject:image];
    // 设置正在刷新状态的动画图片
    [self.firstTable.gifHeader setImages:finishImages forState:MJRefreshHeaderStateRefreshing];
    self.secondTable.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(500, 0, 320, 90)];
//    [self.secondTable.tableFooterView setBackgroundColor:[UIColor blueColor]];
    self.secondTable.tableFooterView.backgroundColor=[UIColor blueColor];
}

-(void)addTableViewFooter{
    [self.firstTable addGifFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    // 设置正在刷新状态的动画图片
    NSMutableArray *refreshingImages = [NSMutableArray array];
    UIImage *image = [UIImage imageNamed:@"normal.png"];
    [refreshingImages addObject:image];
    image = [UIImage imageNamed:@"pulling.png"];
    [refreshingImages addObject:image];
    image = [UIImage imageNamed:@"finish.png"];
    [refreshingImages addObject:image];
    self.firstTable.gifFooter.refreshingImages = refreshingImages;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)userLogin:(id)sender {
    self.logview = [[LoginView alloc]initWithNibName:@"LoginView" bundle:nil];
    [self.navigationController pushViewController:self.logview animated:YES];
}

#pragma reloadData and loadMoreData
-(void)loadNewData{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.firstTable reloadData];
        
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.firstTable.header endRefreshing];
    });
}
-(void)loadMoreData{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.firstTable reloadData];
        
        // 拿到当前的上拉刷新控件，结束刷新状态
        [self.firstTable.footer endRefreshing];
    });
}
@end
